//
//  StoreViewController.m
//  Cheirapsis
//
//  Created by wangning on 15/11/3.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "StoreViewController.h"
#import "AssessViewController.h"
#import "TimeChooseViewController.h"
#import "Define.h"
#import "PageView.h"
#import "iToast.h"

@interface StoreViewController ()

@end

@implementation StoreViewController{

    UILabel *objLabel;
    long k;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _dic[@"storeName"];
    _dataSource = [[NSMutableArray alloc]initWithArray:@[
                                                         @{
                                                             @"chooseNumber":@"0"
                                                             },
                                                         @{
                                                             @"chooseNumber":@"0"
                                                             },
                                                         @{
                                                             @"chooseNumber":@"0"
                                                             }
                                                         ]];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arr = _dic[@"storeServiceType"];
    return arr.count+3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"identifier%ld",(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    if (indexPath.row == 0) {

        UIImageView *titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(15*F_WIDTH, 8*F_HEIGHT, 25*F_WIDTH, 20*F_HEIGHT)];
        titleImage.image = [UIImage imageNamed:@"小电话"];
        
        UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(280*F_WIDTH, 10*F_HEIGHT, 13*F_WIDTH, 20*F_HEIGHT)];
        backImage.image = [UIImage imageNamed:@"蓝色回箭头"];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(41*F_WIDTH, 8*F_HEIGHT, 230*F_WIDTH, 25*F_HEIGHT)];
        titleLabel.text = _dic[@"storeTel"];
        titleLabel.font = [UIFont systemFontOfSize:13*F_WIDTH];
        
        [cell.contentView addSubview:titleImage];
        [cell.contentView addSubview:backImage];
        [cell.contentView addSubview:titleLabel];
    }else if (indexPath.row == 1){
        UIImageView *titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(15*F_WIDTH, 23*F_HEIGHT, 25*F_WIDTH, 20*F_HEIGHT)];
        titleImage.image = [UIImage imageNamed:@"小地址"];
        
        UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(280*F_WIDTH, 23*F_HEIGHT, 13*F_WIDTH, 20*F_HEIGHT)];
        backImage.image = [UIImage imageNamed:@"蓝色回箭头"];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(41*F_WIDTH, 15*F_HEIGHT, 230*F_WIDTH, 40*F_HEIGHT)];
        titleLabel.text = _dic[@"storeAddress"];
        titleLabel.font = [UIFont systemFontOfSize:13*F_WIDTH];
        
        [cell.contentView addSubview:titleImage];
        [cell.contentView addSubview:backImage];
        [cell.contentView addSubview:titleLabel];
    }else if (indexPath.row == 2){
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(41*F_WIDTH, 8*F_HEIGHT, 230*F_WIDTH, 50*F_HEIGHT)];
        titleLabel.text = _dic[@"storeDescripition"];
        titleLabel.font = [UIFont systemFontOfSize:13*F_WIDTH];
        titleLabel.numberOfLines = 0;
        
        
        [cell.contentView addSubview:titleLabel];
    }else{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15*F_WIDTH, 8*F_HEIGHT, 250*F_WIDTH, 20*F_HEIGHT)];
        label.font = [UIFont systemFontOfSize:13*F_WIDTH];
        label.text = [NSString stringWithFormat:@"%@：%@",_dic[@"storeServiceType"][indexPath.row -3],_dic[@"storeServiceTypeIntroduce"][indexPath.row -3]];

        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(280*F_WIDTH, 8*F_HEIGHT, 20*F_WIDTH, 20*F_HEIGHT)];
        imageView.image = [UIImage imageNamed:@"圆圈"];
        imageView.highlightedImage = [UIImage imageNamed:@"圆圈点击"];
        NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row - 3];
        imageView.highlighted = [dic[@"chooseNumber"] boolValue];
        
        
        [cell.contentView addSubview:label];
        [cell.contentView addSubview:imageView];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 40*F_HEIGHT;
    }else if (indexPath.row == 1){
        return 65*F_HEIGHT;
    }else if (indexPath.row == 2){
        return 60*F_HEIGHT;
    }else{
        return 40;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2) {
        return;
    }else{
    [_dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = [_dataSource objectAtIndex:idx];
        NSMutableDictionary *dicMutable = [[NSMutableDictionary alloc] initWithDictionary:dic];
        [dicMutable setValue:@"0" forKey:@"chooseNumber"];
        //替换数组的第idx个元素
        [_dataSource replaceObjectAtIndex:idx withObject:dicMutable];
    }];

    NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row-3];
    NSMutableDictionary *dicMutable = [[NSMutableDictionary alloc] initWithDictionary:dic];
    [dicMutable setValue:@"1" forKey:@"chooseNumber"];
    //替换数组的第idx个元素
    [_dataSource replaceObjectAtIndex:(indexPath.row -3) withObject:dicMutable];
    k = indexPath.row -2;
    [_tableView reloadData];
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 195*F_HEIGHT)];
    headerView.backgroundColor = ALL233COROL;
    //设定广告页 Begin
//    NSArray *imageArray = [[NSArray alloc] initWithObjects:@"大连_1",nil];
    PageView *pageView = [[PageView alloc] initPageViewFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150*F_HEIGHT)];
    pageView.isWebImage = YES;
    pageView.imageArray = _dic[@"storeImageArr"];
    pageView.duration = 3.0;
    
    [headerView addSubview:pageView];
    //设定广告页 End
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20*F_WIDTH, 160*F_HEIGHT, 280*F_WIDTH, 25*F_HEIGHT)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"评价数" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15*F_WIDTH];
    [btn addTarget:self action:@selector(goToAssess) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:btn];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 195*F_HEIGHT;
}

- (void)goToAssess{
    AssessViewController *assess = [[AssessViewController alloc] init];
    [self.navigationController pushViewController:assess animated:YES];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10*F_HEIGHT)];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20*F_WIDTH, 0, 280*F_WIDTH, 35*F_HEIGHT)];
    btn.backgroundColor = [UIColor colorWithRed:159/255.0 green:203/255.0 blue:239/255.0 alpha:1];
    [btn setTitle:@"立即预约" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(goToTime) forControlEvents:UIControlEventTouchUpInside];
    
    [footerView addSubview:btn];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 35*F_HEIGHT;
}

- (void)goToTime{
    if (![[NSUserDefaults standardUserDefaults] valueForKey:@"userName"]) {
        [[iToast makeText:@"您还没有登录，无法预约"] show];
        return;
    }
    if (k == 0) {
        [[iToast makeText:@"请选择服务类型"] show];
        return;
    }
    TimeChooseViewController *time = [[TimeChooseViewController alloc] init];
    time.dic = _dic;
    time.k = k-1;
    [self.navigationController pushViewController:time animated:YES];
}

@end
